package com.qilin.hscs.hm.mqtt.config;

import org.eclipse.paho.client.mqttv3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by 10188 on 2018/3/20.
 */
@Component
public class CASUtil implements Doubleflush {

    @Autowired
    private MqttMessage message;

    private MqttTopic topic;

    @Override
    public void send(MqttClient client, String messageStr, String topicStr) throws MqttException {
        //设置消息的内容
        message.setPayload(messageStr.getBytes());
        topic = client.getTopic(topicStr);
        MqttDeliveryToken token = topic.publish(message);
        token.waitForCompletion();
        client.disconnect();

    }

    @Override
    public void receive(MqttClient client, String topicStr, MqttCallback callback) throws MqttException {
            client.subscribe(topicStr);
            client.setCallback(callback);
    }
}
